﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Service.Email;
using System.Data;


namespace Enterprise.UI.Web.Modules.WebEmail
{
    public partial class Setting : Enterprise.Service.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindEmail();
            }
        }

        protected void BindEmail()
        {            
            EmailService es = new EmailService();
            bool t = false;
            DataSet ds = es.GetMailSetting(Enterprise.Infrastructure.Utility.Get_UserId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                tb_Email.Text = ds.Tables[0].Rows[0]["EMAIL"].ToString();       
                tb_Pwd.Text = ds.Tables[0].Rows[0]["LOGINPASSWORD"].ToString();
                tb_popserver.Text = ds.Tables[0].Rows[0]["POPSERVER"].ToString();
                tb_smtpserver.Text = ds.Tables[0].Rows[0]["SMTPSERVER"].ToString();
                tb_popserver_port.Text = ds.Tables[0].Rows[0]["POPPORT"].ToString();
                tb_smtpserver_port.Text = ds.Tables[0].Rows[0]["SMTPPORT"].ToString();

                t = es.Exist(tb_Email.Text);//收件箱是否有邮件 

                if (ds.Tables[0].Rows[0]["ISENABLE"].ToString() == "1"&&t)
                {
                    tb_State.Text = "接收正常";
                    tb_State.ForeColor = System.Drawing.Color.Green;
                }
                if (ds.Tables[0].Rows[0]["ISENABLE"].ToString() == "1"&&t==false)
                {
                    tb_State.Text = "等待验证....";
                    tb_State.ForeColor = System.Drawing.Color.Green;
                }
                if (ds.Tables[0].Rows[0]["ISENABLE"].ToString() == "0")
                {
                    tb_State.Text = "邮箱配置发生错误，请重新配置";
                    tb_State.ForeColor = System.Drawing.Color.Red;
                }
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            //保存数据
            EmailService es = new EmailService();
            es.SaveSet(Enterprise.Infrastructure.Utility.Get_UserId,
                tb_Email.Text.Trim(),
                tb_Pwd.Text,
                tb_popserver.Text,
                tb_smtpserver.Text,
                int.Parse(tb_popserver_port.Text),
                int.Parse(tb_smtpserver_port.Text),
                1);
            Enterprise.Infrastructure.Utility.ShowMsg(this.Page, "ok!");
            BindEmail();
        }
    }
}